﻿@page "/General/ChartToImage"
@using LiveChartsCore
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore.SkiaSharpView;
@using LiveChartsCore.SkiaSharpView.SKCharts;

<div style="max-height: 400px">
    <CartesianChart
        @ref="_cartesianChart"
        Series="@cartesianSeries">
    </CartesianChart>
</div>

<div style="max-height: 400px">
    <PieChart
        @ref="_pieChart"
        Series="@pieSeries">
    </PieChart>
</div>

@code {
    private CartesianChart _cartesianChart = null!;
    private PieChart _pieChart = null!;

    private double[] values1 = new double[] { 2, 5, 4 };
    private double[] values2 = new double[] { 3, 1, 6 };
    private double[] pieValues1 = new double[] { 2 };
    private double[] pieValues2 = new double[] { 4 };
    private double[] pieValues3 = new double[] { 1 };

    private ISeries[] cartesianSeries => new ISeries[]
    {
        new ColumnSeries<double> { Values = values1 },
        new LineSeries<double> { Values = values2 }
    };

    private ISeries[] pieSeries => new ISeries[]
    {
        new PieSeries<double> { Values = pieValues1 },
        new PieSeries<double> { Values = pieValues2 },
        new PieSeries<double> { Values = pieValues3 }
    };

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        CreateImageFromCartesianControl();
        CreateImageFromPieControl();
    }

    private void CreateImageFromCartesianControl()
    {
        var chartControl = _cartesianChart;
        var skChart = new SKCartesianChart(chartControl);
        skChart.SaveImage("CartesianImageFromControl.png");
    }

    private void CreateImageFromPieControl()
    {
        var chartControl = _pieChart;
        var skChart = new SKPieChart(chartControl);
        skChart.SaveImage("PieImageFromControl.png");
    }
}
